#!/usr/bin/env bash
# Copyright (c) 2022 maminjie <canpool@163.com>
# SPDX-License-Identifier: MulanPSL-2.0

usage() {
    echo "usage: bash $0 rpmdir"
}

main() {
    if [[ $# -lt 1 || ! -d "$1" ]]; then
        usage; exit
    fi
    rpmdir="$1"
    rpmlist=$(find "$rpmdir" -name "*.rpm" | sort)
    for r in $rpmlist; do
        srcrpm=$(rpm -qpi $r 2>/dev/null | grep "^Source RPM" | awk '{print $4}')
        if [ -z "$srcrpm" ]; then
            srcrpm=$r
        fi
        echo "$(basename $srcrpm)"
    done
}

main "$@"
